package com.example.order.RocketMq;

import com.alibaba.fastjson.JSON;
import com.example.order.dao.po.Order;
import com.example.order.service.impl.OrderServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.apache.rocketmq.spring.annotation.RocketMQMessageListener;
import org.apache.rocketmq.spring.core.RocketMQListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

@Slf4j
@Component
@RocketMQMessageListener(
        consumerGroup = "order-consumer",
        topic = "ORDER_TOPIC",
        selectorExpression = "CANCEL_ORDER_TAG" // 过滤多个tag
)
public class MQOrderListener implements RocketMQListener<Order> {
    @Autowired
    private OrderServiceImpl ordersService;
    // 监听到消息就会执行此方法
    @Override
    public void onMessage(Order orders) {
        log.info("监听到消息：orders={}", JSON.toJSONString(orders));
        var orderId = orders.getId();
        if(ordersService.deleteOrder(orderId)){
            log.info("成功取消订单 ：" + orderId);
        }
    }
}

